﻿var app = angular.module('myAPP');
app.controller('RegisterController', function ($scope, $rootScope, $http, $location, $modal, $window) {
    $scope.testId = 1;
    $scope.listUser = [];


    $scope.open = function () {
        $scope.opened = true;
    };

    //    $scope.resetValue = function () {
    //        $scope.title = '';
    //        $scope.cusCode = '';
    //        $scope.name = '';
    //        $scope.zipCode = '';
    //        $scope.address = '';
    //        $scope.idCard = '';
    //        $scope.mobile = '';
    //        $scope.email = '';
    //        $scope.password = '';
    //        $scope.saving = false;
    //        $scope.searching = false;
    //        $scope.submitted = false;
    //        $scope.search_submitted = false;
    //        $scope.modeRegist = false;
    //    };

    $scope.resetValue = function () {
        $scope.saving = false;
        $scope.searching = false;
        $scope.submitted = false;
        $scope.search_submitted = false;
        $scope.modeRegist = false;
        $scope.mobile = '';
        $scope.emailPrimary = '';
        $scope.emailSecondary = '';
        $scope.passwordUser = '';
        $scope.passwordConfirm = '';
        $scope.title = '';
        $scope.name = '';
        $scope.zipCode = '';
        $scope.address = '';
    };

    $scope.redirectReport = function () {
        $window.open('/../../UI/Report/TestReport2.aspx?testId=' + $scope.testId, '_blank');
    };

//    $scope.loadTestUser = function () {
//        $http({
//            url: "../../PaydSw.asmx/GetListUser",
//            dataType: 'json',
//            method: 'POST',
//            data: '',
//            headers: {
//                "Content-Type": "application/json"
//            }
//        }).success(function (response) {
//            var data = JSON.parse(response.d);
//            if (data.Result == 1) {
//                //console.log(data.objData);
//                $scope.listUser = data.objData;
//            }


//        })
//        .error(function (error) {

//            var ErrorMessage = 'error at loadTestUser at RegisterController ' + error.ExceptionType + ' ' + error.Message + ' ' + error.StackTrace;
//            alert(ErrorMessage);
//            $scope.$parent.doselHome();
//            $location.path('/home');
//        });
//    };


    $scope.init = function () {
        $scope.modeRegist = false;
        $scope.msg = {};
        $scope.saving = false;
        $scope.searching = false;
        $scope.submitted = false;
        $scope.search_submitted = false;
        $scope.$parent.doselProfile();
        $scope.loadTestUser();
    };

    $scope.searchUserAction = function (valid) {
        if (valid) {
            var varIdCard = $scope.idCard;
            if (validate_thai_citizen_id(varIdCard) == false) {
                $scope.$parent.openAlert(0, "แจ้งเตือน", "เลขที่บัตรประชาชนไม่ถูกต้อง");
            }
            else {
                $http({
                    url: "../../PaydSw.asmx/PaydCheckUser",
                    dataType: 'json',
                    method: 'POST',
                    data: {
                        idCard: varIdCard,
                        language: 'TH'
                    },
                    headers: {
                        "Content-Type": "application/json"
                    }
                }).success(function (response) {
                    var data = JSON.parse(response.d);
                    if (data.Result == 0 || data.Result == 2) {
                        $scope.$parent.openAlert(data.Result, data.MessageTitle, data.MessageContent);
                    }
                    else if (data.Result == 3) {
                        $scope.$parent.openAlertLogin(data.Result, data.MessageTitle, data.MessageContent);
                    }
                    else {

                        $http({
                            url: "../../PaydSw.asmx/GetUserByIdCard",
                            dataType: 'json',
                            method: 'POST',
                            data: {
                                idCard: varIdCard,
                                language: 'TH',
                                sorting: ''
                            },
                            headers: {
                                "Content-Type": "application/json"
                            }
                        }).success(function (response) {
                            var data = JSON.parse(response.d);
                            if (data.Result == 1) {
                                $scope.memberId = data.objData.MemberId;
                                $scope.title = data.objData.Title;
                                $scope.name = data.objData.Name;
                                $scope.zipCode = data.objData.ZipCode;
                                $scope.address = data.objData.Addr1 + ' ' + data.objData.Addr2;
                                $scope.mobile = data.objData.MobileNo;
                                $scope.emailPrimary = data.objData.EmailPrimary;
                                $scope.emailSecondary = data.objData.EmailSecondary;
                                $scope.passwordUser = '';
                                $scope.passwordConfirm = '';
                                $scope.modeRegist = true;
                            }


                        })
                        .error(function (error) {

                            var ErrorMessage = 'error at searchUserAction at RegisterController ' + error.ExceptionType + ' ' + error.Message + ' ' + error.StackTrace;
                            alert(ErrorMessage);
                            $scope.$parent.doselHome();
                            $location.path('/home');
                        });

                    }

                })
                .error(function (error) {
                    var dataError = JSON.parse(error.responseText);
                    var ErrorMessage = 'error at searchUserAction at RegisterController ' + dataError.ExceptionType + ' ' + dataError.Message + ' ' + dataError.StackTrace;
                    alert(ErrorMessage);
                    $scope.$parent.doselHome();
                    $location.path('/home');
                });
            }
        }
        else {
            $scope.search_submitted = true;
        }
    };

    $scope.registerAction = function (valid) {
        if (valid) {
            if ($scope.emailPrimary == $scope.emailSecondary) {
                $scope.$parent.openAlert(0, "ข้อความเตือน", "อีเมล์หลักและอีเมล์สำรองต้องไม่เท่ากัน");
            }
            else if ($scope.passwordUser != $scope.passwordConfirm) {
                $scope.$parent.openAlert(0, "ข้อความเตือน", "รหัสผ่านและรหัสผ่านยืนยันไม่ตรงกัน");
            }
            else {
                
                var paydUserObj = {};
                paydUserObj.MemberId = $scope.memberId;
                paydUserObj.IdCard = $scope.idCard;
                paydUserObj.Name = $scope.name;
                paydUserObj.Title = $scope.title;
                paydUserObj.MobileNo = $scope.mobile;
                paydUserObj.EmailPrimary = $scope.emailPrimary;
                paydUserObj.EmailSecondary = $scope.emailSecondary;
                paydUserObj.PasswordUser = $scope.passwordUser;
                
                $http({
                    url: "../../PaydSw.asmx/SavePaydUser",
                    dataType: 'json',
                    method: 'POST',
                    data: {
                        paydUser: paydUserObj,
                        language: $scope.$parent.language
                    },
                    headers: {
                        "Content-Type": "application/json"
                    }
                }).success(function (response) {
                    var data = JSON.parse(response.d);
                    if (data.Result == 1) {
                        $scope.$parent.openAlertLogin(data.Result, data.MessageTitle, data.MessageContent);
                    }
                    else {
                        $scope.$parent.openAlert(data.Result, data.MessageTitle, data.MessageContent);
                    }

                })
                .error(function (error) {
                    var ErrorMessage = 'error at registerAction at RegisterController ' + error.ExceptionType + ' ' + error.Message + ' ' + error.StackTrace;
                    alert(ErrorMessage);
                    $scope.$parent.doselHome();
                    $location.path('/home');
                });
            }
        }
        else {
            $scope.submitted = true;
        }
    };



    //    $scope.save = function (valid) {

    //        if (valid) {
    //            $scope.saving = true;
    //            var paydMemberObj = {};
    //            paydMemberObj.UserName = $scope.username;
    //            paydMemberObj.Password = $scope.username;
    //            paydMemberObj.TitleId = $scope.titleId;
    //            paydMemberObj.FirstName = $scope.firstName;
    //            paydMemberObj.LastName = $scope.lastName;
    //            paydMemberObj.FirstNameEng = '';
    //            paydMemberObj.LastNameEng = '';
    //            paydMemberObj.IdentityNum = $scope.identityNum;
    //            //            paydMemberObj.BirthDate = moment($scope.birthDate).valueOf();
    //            paydMemberObj.BirthDate = moment($scope.birthDate).format("YYYY-MM-DD");
    //            paydMemberObj.BirthDateText = moment($scope.birthDate).format("YYYY-MM-DD");
    //            paydMemberObj.Sex = $scope.gender;
    //            paydMemberObj.TelNo = $scope.telNo;
    //            paydMemberObj.Email = $scope.email;
    //            paydMemberObj.MobileNo = $scope.mobileNo;
    //            paydMemberObj.FaxNo = $scope.faxNo;
    //            paydMemberObj.HouseNo = $scope.houseNo;
    //            paydMemberObj.VillageNo = $scope.villageNo;
    //            paydMemberObj.VillageName = $scope.villageName;
    //            paydMemberObj.Soi = $scope.soi;
    //            paydMemberObj.Road = $scope.road;
    //            paydMemberObj.DistrictId = $scope.districtId;
    //            paydMemberObj.MaritalStatus = $scope.maritalStatus;

    //            $http({
    //                url: "../../PaydSw.asmx/SavePaydMember",
    //                dataType: 'json',
    //                method: 'POST',
    //                data: {
    //                    paydMember: paydMemberObj,
    //                    language: 'TH'
    //                },
    //                headers: {
    //                    "Content-Type": "application/json"
    //                }
    //            }).success(function (response) {
    //                var data = JSON.parse(response.d);

    //                $scope.saving = false;
    //                $scope.openAlert(data.Result, data.MessageTitle, data.MessageContent);
    //            })
    //            .error(function (error) {
    //                var dataError = JSON.parse(error.responseText);
    //                var ErrorMessage = 'error at save in SavePaydMember at save in RegisterController ' + dataError.ExceptionType + ' ' + dataError.Message + ' ' + dataError.StackTrace;
    //                $scope.saving = false;
    //                alert(ErrorMessage);
    //                $scope.$parent.doselHome();
    //                $location.path('/home');
    //            });
    //        }
    //        else {
    //            $scope.submitted = true;
    //        }
    //    };

    //    $scope.doLogin = function (varUserName, varPassword) {
    //        $http({
    //            url: "../../PaydSw.asmx/CheckMemberLogin",
    //            dataType: 'json',
    //            method: 'POST',
    //            data: {
    //                username: varUserName,
    //                password: varPassword
    //            },
    //            headers: {
    //                "Content-Type": "application/json"
    //            }
    //        }).success(function (response) {
    //            var data = JSON.parse(response.d);
    //            if (data.Result == 1) {
    //                $scope.$parent.checkExistLogin();
    //            }
    //            $scope.$parent.doselProfile();
    //            $location.path('/Profile');
    //        })
    //            .error(function (error) {
    //                var dataError = JSON.parse(error.responseText);
    //                var ErrorMessage = 'error at CheckMemberLogin in doLogin  at RegisterController ' + dataError.ExceptionType + ' ' + dataError.Message + ' ' + dataError.StackTrace;
    //                alert(ErrorMessage);
    //                $scope.$parent.doselHome();
    //                $location.path('/home');
    //            });
    //    };

});